package com.smart.translator.ota;

import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * OTA工具类 - 提供固件文件验证和读取功能
 */
public class OtaUtils {
    private static final String TAG = "OtaUtils";
    
    /**
     * 验证固件文件是否有效
     */
    public static boolean isValidFirmwareFile(String filePath) {
        if (filePath == null || filePath.isEmpty()) {
            Log.w(TAG, "文件路径为空");
            return false;
        }
        
        File file = new File(filePath);
        if (!file.exists()) {
            Log.w(TAG, "文件不存在: " + filePath);
            return false;
        }
        
        if (!file.canRead()) {
            Log.w(TAG, "文件无法读取: " + filePath);
            return false;
        }
        
        // 检查文件大小
        long fileSize = file.length();
        if (fileSize == 0) {
            Log.w(TAG, "文件大小为0: " + filePath);
            return false;
        }
        
        if (fileSize > 1024 * 1024 * 10) { // 10MB限制
            Log.w(TAG, "文件过大: " + fileSize + " 字节");
            return false;
        }
        
        // 检查文件扩展名
        String fileName = file.getName().toLowerCase();
        if (!fileName.endsWith(".bin") && !fileName.endsWith(".hex") && 
            !fileName.endsWith(".fw") && !fileName.endsWith(".img")) {
            Log.w(TAG, "不支持的文件格式: " + fileName);
            return false;
        }
        
        Log.d(TAG, "固件文件验证通过: " + filePath + ", 大小: " + fileSize + " 字节");
        return true;
    }
    
    /**
     * 读取固件文件内容
     */
    public static byte[] readFirmwareFile(String filePath) throws IOException {
        if (!isValidFirmwareFile(filePath)) {
            throw new IOException("固件文件无效: " + filePath);
        }
        
        File file = new File(filePath);
        byte[] data = new byte[(int) file.length()];
        
        try (FileInputStream fis = new FileInputStream(file)) {
            int bytesRead = fis.read(data);
            if (bytesRead != data.length) {
                throw new IOException("文件读取不完整: 期望 " + data.length + " 字节，实际读取 " + bytesRead + " 字节");
            }
        }
        
        Log.d(TAG, "固件文件读取成功: " + filePath + ", 大小: " + data.length + " 字节");
        return data;
    }
    
    /**
     * 计算CRC32校验值
     */
    public static int calculateCRC32(byte[] data) {
        if (data == null || data.length == 0) {
            return 0;
        }
        
        int crc = 0xFFFFFFFF;
        for (byte b : data) {
            crc = crc ^ (b & 0xFF);
            for (int i = 0; i < 8; i++) {
                if ((crc & 1) != 0) {
                    crc = (crc >>> 1) ^ 0xEDB88320;
                } else {
                    crc = crc >>> 1;
                }
            }
        }
        return crc ^ 0xFFFFFFFF;
    }
    
    /**
     * 字节数组转十六进制字符串
     */
    public static String bytesToHexString(byte[] data) {
        if (data == null || data.length == 0) {
            return "";
        }
        
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {
            sb.append(String.format("%02X", b & 0xFF));
        }
        return sb.toString();
    }
} 